package com.qs;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * 测试验证spring支持的cglib动态代理
 *
 * @author qinyupeng
 * @since 2021-05-30 13:56:55
 */
public class TestCglib {

	public static void main(String[] args) {
		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(OrderService.class);
		// 自定义增强结果，可以实现MethodInterceptor接口，MethodInterceptor接口实现了Callback接口。
		enhancer.setCallback(new MethodInterceptor() {
			@Override
			public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
				System.out.println("代理类开始。。");
				Object result = methodProxy.invokeSuper(o, null);
				System.out.println("执行结果：result=" + result);
				return result;
			}
		});

		OrderService orderService = (OrderService) enhancer.create();
		String orderId = orderService.doOrder();
		System.out.println("===>>" + orderId);
	}
}
